package store

import (
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"gitee.com/zaiqiang231/lovesport-user-service/app/config"
	"github.com/go-redis/redis/v8"
	"github.com/pkg/errors"
	"sync"
)

var (
	redisStore     redis.UniversalClient
	onceRedisStore sync.Once
)

func InitRedisStore(opts *config.RedisConfig) (redis.UniversalClient, error) {
	onceRedisStore.Do(func() {
		baseLog.Infof("InitRedisStore %v-%v", opts.Addr, opts.Password)
		redisStore = redis.NewClient(&redis.Options{
			Addr:     opts.Addr,
			Password: opts.Password, // no password set
			DB:       0,             // use default DB
		})
	})

	if redisStore == nil {
		return nil, errors.New("mysql failed to get store")
	}
	return redisStore, nil
}

func GetRedisStore() redis.UniversalClient {
	if redisStore == nil {
		baseLog.Errorf("datastore is %v", datastore)
	}
	return redisStore
}
